import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import NotFound from '../views/NotFound.vue'
import Store from '../store/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/discover'
  },
  {
    path: '/discover',
    name: 'discover',
    redirect: '/discover/home',
    component: () => import(/* webpackChunkName: "discover" */ '../views/Discover.vue'),
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import( /* webpackChunkName: "home" */ '../views/Home.vue')
      },
      {
        path: 'toplist:id?',
        name: 'toplist',
        component: () => import( /* webpackChunkName: "toplist" */ '../components/discover/Toplist.vue')
      },
      {
        path: 'songmenu:cat?',
        name: 'songmenu',
        component: () => import( /* webpackChunkName: "songmenu" */ '../components/discover/SongMenu.vue')
      },
      {
        path: 'djradio',
        name: 'djradio',
        component: () => import( /* webpackChunkName: "djradio" */ '../components/discover/Djradio.vue')
      },
      {
        path: 'artist',
        name: 'artist',
        redirect: '/discover/artist/list',
        component: () => import( /* webpackChunkName: "artist" */ '../components/discover/Artist.vue'),
        children: [
          {
            path: 'hot',
            name: 'hot',
            component: () => import( /* webpackChunkName: "hot" */ '../components/Artist/HotArtist.vue')
          },
          {
            path: 'settle',
            name: 'settle',
            component: () => import( /* webpackChunkName: "settle" */ '../components/Artist/SettleArtist.vue')
          },
          {
            path: 'list',
            name: 'list',
            component: () => import( /* webpackChunkName: "list" */ '../components/Artist/ArtistList.vue')
          },
        ]
      },
      {
        path: 'album',
        name: 'album',
        component: () => import( /* webpackChunkName: "djradio" */ '../components/discover/Album.vue')
      },
    ]
  },
  {
    path: '/mymusic:id?',
    name: 'mymusic',
    component: () => import(/* webpackChunkName: "mymusic" */ '../views/Mymusic.vue')
  },
  {
    path: '/friend',
    name: 'friend',
    component: () => import(/* webpackChunkName: "friend" */ '../views/Friend.vue')
  },
  {
    path: '/product',
    name: 'product',
    component: () => import(/* webpackChunkName: "product" */ '../views/Product.vue')
  },
  {
    path: '/musician',
    name: 'musician',
    component: () => import(/* webpackChunkName: "musician" */ '../views/Musician.vue')
  },
  {
    path: '/download',
    name: 'download',
    component: () => import(/* webpackChunkName: "musician" */ '../views/Download.vue')
  },
  {
    path: '/playlist/:id',
    name: 'playlist',
    component: () => import(/* webpackChunkName: "playlist" */ '../components/Detail/MusicDetail.vue')
  },
  {
    path: '/artlist/:id',
    name: 'artlist',
    redirect: '/artlist/:id/hotsong',
    component: () => import(/* webpackChunkName: "artlist" */ '../components/Detail/ArtistDetail.vue'),
    children: [
      {
        path: 'hotsong',
        name: 'hotsong',
        component: () => import(/* webpackChunkName: "hotsong" */ '../components/ArtistDetail/HotSong.vue')
      },
      {
        path: 'artistalbum',
        name: 'artistalbum',
        component: () => import(/* webpackChunkName: "artistalbum" */ '../components/ArtistDetail/ArtistAlbum.vue')
      },
      {
        path: 'artistmv',
        name: 'artistmv',
        component: () => import(/* webpackChunkName: "artistmv" */ '../components/ArtistDetail/ArtistMv.vue')
      },
      {
        path: 'artistdesc',
        name: 'artistdesc',
        component: () => import(/* webpackChunkName: "artistdesc" */ '../components/ArtistDetail/ArtistDesc.vue')
      },
    ]
  },
  {
    path: '/mvlist/:id',
    name: 'mvlist',
    component: () => import(/* webpackChunkName: "mvlist" */ '../components/Detail/MvDetail.vue')
  },
  {
    path: '/videolist/:id',
    name: 'videolist',
    component: () => import(/* webpackChunkName: "videolist" */ '../components/Detail/VideoDetail.vue')
  },
  {
    path: '/albumlist/:id',
    name: 'albumlist',
    component: () => import(/* webpackChunkName: "albumlist" */ '../components/Detail/AlbumDetail.vue')
  },
  {
    path: '/songlist/:id',
    name: 'songlist',
    component: () => import(/* webpackChunkName: "songlist" */ '../components/Detail/SongDetail.vue')
  },
  {
    path: '/user/:id',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue')
  },
  {
    path: '/program/:id',
    name: 'program',
    component: () => import(/* webpackChunkName: "program" */ '../components/Detail/ProgramDetail.vue')
  },
  {
    path: '/djradio/:id',
    name: 'djradio',
    component: () => import(/* webpackChunkName: "program" */ '../components/Detail/DjRadioDetail.vue')
  },
  {
    path: '/search/:keyword',
    name: 'search',
    redirect: '/search/:keyword/single',
    component: () => import( /* webpackChunkName: "search" */ '../views/Search.vue'),
    children: [
      {
        path: 'single',
        name: 'single',
        component: () => import(/* webpackChunkName: "single" */ '../components/Search/Single.vue')
      },
      {
        path: 'artists',
        name: 'artists',
        component: () => import(/* webpackChunkName: "artists" */ '../components/Search/Artists.vue')
      },
      {
        path: 'albums',
        name: 'albums',
        component: () => import(/* webpackChunkName: "albums" */ '../components/Search/Albums.vue')
      },
      {
        path: 'videos',
        name: 'videos',
        component: () => import(/* webpackChunkName: "videos" */ '../components/Search/Videos.vue')
      },
      {
        path: 'playlists',
        name: 'playlists',
        component: () => import(/* webpackChunkName: "playlists" */ '../components/Search/Playlists.vue')
      },
      {
        path: 'djradios',
        name: 'djradios',
        component: () => import(/* webpackChunkName: "djradios" */ '../components/Search/DjRadios.vue')
      },
      {
        path: 'users',
        name: 'users',
        component: () => import(/* webpackChunkName: "users" */ '../components/Search/Users.vue')
      },
    ]
  },
  {
    path: '*',
    name: 'NotFound',
    component: NotFound,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  if(to.path === '/mymusic'){
    let token = localStorage.getItem('user')
    if(token){
      Store.commit("isHeaderShow", true);
      Store.commit("isLoginShow", true);
      next();
    }else{
      // 提示
      let obj = {content: "请先登录!", icon: "info"}
      Store.dispatch('AsyncToast', obj)
    }
    return
  }
  next()
})

export default router
